﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// Bussiness components for order
    /// </summary>
    public class Order
    {
        //Order state
        private const int ORDERSTATE_UNDEAL = 0;
        private const int ORDERSTATE_DEALED = 1;
        private const int ORDERSTATE_CANCEL = 2;

        private static readonly IOrder orderDal = DALFactory.DataAccess.CreateOrderDAL();

        /// <summary>
        /// Get the order
        /// </summary>
        /// <param name="orderId">Order id</param>
        /// <returns>The order</returns>
        public OrderInfo GetOrder(int orderId)
        {
            OrderInfo order = orderDal.SelectOrder(orderId);
            if (order != null)
            {
                return order;
            }
            else
            {
                throw new ApplicationException("No such order.");
            }
        }

        /// <summary>
        /// Get orders 
        /// </summary>
        /// <param name="username">Username</param>
        /// <returns>Collection of orders</returns>
        public IList<OrderInfo> GetOrders(string username)
        {
            return orderDal.SelectByUsername(username);
        }

        /// <summary>
        /// Get orders
        /// </summary>
        /// <param name="state">Username</param>
        /// <returns>Collection of orders</returns>
        private IList<OrderInfo> GetOrders(int state)
        {
            return orderDal.SelectByState(state);
        }

        public IList<OrderInfo> GetUnDealedOrders()
        {
            return orderDal.SelectByState(ORDERSTATE_UNDEAL);
        }

        public IList<OrderInfo> GetDealedOrders()
        {
            return orderDal.SelectByState(ORDERSTATE_DEALED);
        }

        public IList<OrderInfo> GetCanceledOrders()
        {
            return orderDal.SelectByState(ORDERSTATE_CANCEL);
        }


        /// <summary>
        /// Turn the shopping cart into order
        /// </summary>
        /// <param name="username">Username</param>
        /// <param name="cart">Shopping cart</param>
        public void AddOrders(string username, string shipName,string shipAddress,string shipPhone ,Cart cart)
        {
            if (cart.Count != 0)
            {
                DateTime orderCreateTime = DateTime.Now;
                OrderInfo order = new OrderInfo(0, orderCreateTime, username, shipName, shipAddress, shipPhone, ORDERSTATE_UNDEAL);
                order.LineItems = cart.GetOrderLineItems();
                orderDal.InsertOrder(order);
            }
        }

        /// <summary>
        /// Deal the order 
        /// </summary>
        /// <param name="orderId">Order id</param>
        public void DealOrder(int orderId)
        {
            OrderInfo order = GetOrder(orderId);
            if (order.OrderState == ORDERSTATE_UNDEAL)
            {
                orderDal.DealOrder(orderId);
            }
            else
            {
                throw new ApplicationException("Order has been dealed.");
            }
        }

        /// <summary>
        /// Cancel the order
        /// </summary>
        /// <param name="orderId">Order id</param>
        public void CancelOrder(int orderId)
        {
            OrderInfo order = GetOrder(orderId);
            if (order.OrderState == ORDERSTATE_UNDEAL)
            {
                orderDal.CancelOrder(orderId);
            }
            else
            {
                throw new ApplicationException("Order has been dealed.");
            }
        }
    }
}
